﻿/**********************************************
 * 功能名称：中间产品相关参数信息录入页面
编写人：  杨莹
建立时间： 杨 2008年11月8日
说明：
加载页面时，根据当前用户ID查询出责任部门，列表显示其作业中心列表。
作业中心列表中选择后，列表出作业中心中间体；
选择作业中心中间体，录入当前中间体的转出数量。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;


namespace Hisun.View.PIPDeliverInfo
{
    public partial class PIPDiliverInfo_OnlyAmount : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        string CenterIdArray = "";
        public string alert= "";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
            var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
            DropDownList_Product.Items.Clear();
            FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

            DropDownList_Product.Items.Clear();
            if (CenterIdArray == "")
            {

            }
            else
            {
                string[] CenterIDs = CenterIdArray.Split(',');
                HiddenField_CenterID.Value = CenterIdArray;

                var myProductList = (from a in db.TABC_JS_ProductInProcess 
                                    join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                    where CenterIDs.Contains(a.作业中心ID.ToString())
                                    && a.可用 == true  orderby a.产品ID
                                    select new
                                    {
                                        a.产品ID,
                                        b.产品账户名称
                                    }).Distinct();

               
                foreach(var myProduct in myProductList)
                {
                   DropDownList_Product.Items.Add(new ListItem(myProduct.产品ID+"|"+myProduct.产品账户名称,myProduct.产品ID));
                
                }
            
            }


        }

        }


        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenters = new ChildCenterList(CenterID);

              var myCenterFilters = myCenters.CostCenterlist.Where(p => p.停用 == false);
 

            foreach(var myCenterFilter in myCenterFilters)
            {
                CenterIdArray = CenterIdArray + "," + myCenterFilter.作业中心ID.ToString();

            }


        }


        protected void Button_Query_Click(object sender, EventArgs e)
        {
            if(DropDownList_Product.SelectedValue == "")
            {
                Panel_Main.Visible = false;
               // Response.Write("<script>alert('无效的成本账户ID');</script>");
                alert = "<script>alert('无效的成本账户ID');</script>";
               
            }
            else
            {

                HiddenField_ProductID.Value = DropDownList_Product.SelectedValue.ToString();
                myDataBind(HiddenField_ProductID.Value);

            

            }



           
        }

        private void myDataBind(string myProductID)
        {
            string[] CenterIDs = HiddenField_CenterID.Value.ToString().Split(',');

            var myPIPInfo = from m in db.VABC_JS_ProductInProcessDeliveryDetail
                            where m.产品ID == myProductID
                            && m.会计期间ID == Application[ServerVars.sysApplcationAccountPeriod].ToString()
                            && CenterIDs.Contains(m.作业中心ID.ToString())  orderby m.顺序码
                            select new
                            {
                                m.ID,
                                作业中心 = m.作业中心ID.ToString()+ "|" + m.作业中心名称,
                                中间体 = m.中间体ID + "|" + m.中间体名称,
                                目标作业中心 = m.目标作业中心ID.ToString() + "|" + m.目标中心名称,
                                目标中间体 = m.目标中间体ID + "|" + m.目标中间体名称,
                                m.转出数量,
                                m.时间,
                                m.计量单位,
                                m.操作员ID
                            };
            if (myPIPInfo.Count() == 0)
            {
                Panel_Main.Visible = false;
            }
            else 
            {
                Panel_Main.Visible = true;
                GridView_DiliverInfo.DataSource = myPIPInfo;
                GridView_DiliverInfo.DataBind();  
            }

            
        
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < GridView_DiliverInfo.Rows.Count; i++)
                {
                    int myid = Convert.ToInt32(GridView_DiliverInfo.DataKeys[i]["ID"].ToString());
                    string mynum = ((TextBox)GridView_DiliverInfo.Rows[i].Cells[3].FindControl("TextBox_Amount")).Text;
                    var myrow = (from a in db.TABC_JS_ProductInProcessDeliveryDetail
                                 where a.ID == myid
                                 select a).Single();
                    if (mynum == "")
                    { myrow.转出数量 = 0; }
                    else
                    { myrow.转出数量 = Convert.ToDecimal(mynum); }
                    myrow.时间 = DateTime.Now;
                    myrow.操作员ID = Session[ServerVars.sysSessionUserName].ToString();


                }
                db.SubmitChanges();
              //  Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据成功'); </script>");
                alert = "<script>alert('更新数据成功');</script>";
                myDataBind(HiddenField_ProductID.Value);
            }
            catch
            {
              //  Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新数据出错，请与管理员联系'); </script>");
                alert = "<script>alert('更新数据失败');</script>";
            }
        }
    }
}
